package com.zdy.im.user.dao;


import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sixeco.framework.common.data.BasePage;
import com.sixeco.framework.core.db.IterableConverter;
import com.sixeco.framework.core.db.ext.Query;
import com.sixeco.framework.core.mybatis.MyBatisBaseCacheDao;
import com.sixeco.framework.http.data.vo.ResponseResultList;
import com.sixeco.framework.util.PageUtils;
import com.zdy.im.user.dto.request.ImChatMessageRequestDto;
import com.zdy.im.user.dto.response.ImChatMessageResponseDto;
import com.zdy.im.user.entity.ImChatMessage;
import com.zdy.im.user.mapper.ImChatMessageMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;




@Service("imChatMessageDao")
public class ImChatMessageDao extends MyBatisBaseCacheDao<ImChatMessageMapper, ImChatMessage> {

    @Autowired
    private ImChatMessageMapper  imChatMessageMapper;

    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ImChatMessage>  iPage =  new Query<ImChatMessage>().getPage(params);

        QueryWrapper<ImChatMessage> query =  new QueryWrapper<>();

        IPage<ImChatMessage> page = this.page(iPage,query );

        return new PageUtils(page);
    }


    @Override
    public void saveData(List<Object> list) {
        List<ImChatMessage> datas = new ArrayList<>();
        list.forEach(e -> datas.add(ImChatMessage.build().clone(e)));
        this.saveBatch(datas, BATCH_SIZE);
        }

    /**
     * 通过分页获取ImChatMessage信息实,查找用户和指定用户的聊天历史记录列表（只包括读与未读的）
     * @param basePage BasePage，具体，send_user_id，to_user_id,read_status>=1,global_id >N
     * @return List<ImChatMessageResponseDto>
     * @author suven
     * @date 2021-05-12 17:26:13
     */
    public List<ImChatMessageResponseDto> getImChatMessageByUserIdToIdPage(BasePage basePage){

        List<ImChatMessageResponseDto> resDtoList = new ArrayList<>();
        if(basePage == null){
            return resDtoList;
        }
        //分页对象        PageHelper
        ImChatMessageRequestDto imChatMessageRequestDto = (ImChatMessageRequestDto) basePage.getParamObject();
        if(imChatMessageRequestDto == null){
            return resDtoList;
        }
        Page<ImChatMessage> iPage = new Page<>(basePage.getPageNo(), basePage.getRealPageSize());
        iPage.setSearchCount(false);
//        QueryWrapper<ImChatMessage> queryWrapper = new QueryWrapper<>();
//
//        queryWrapper.le("read_status", 1);//未读和已读聊天记录
//        if(imChatMessageRequestDto.getGlobalId() > 0){
//            queryWrapper.lt("global_id",imChatMessageRequestDto.getGlobalId());
//        }
//        queryWrapper.and(wrapper1 -> wrapper1.eq("to_id",imChatMessageRequestDto.getToId())
//                .eq("send_user_id",imChatMessageRequestDto.getSendUserId())).or(wrapper1 -> wrapper1.eq("send_user_id",imChatMessageRequestDto.getToId())
//                .eq("to_id",imChatMessageRequestDto.getSendUserId()));

        iPage.addOrder(OrderItem.desc("global_id"));
//        IPage<ImChatMessage> page = this.page(iPage, queryWrapper);
        IPage<ImChatMessage> page = imChatMessageMapper.getPageList(iPage, imChatMessageRequestDto);
        if(null == page){
            return resDtoList;
        }

        List<ImChatMessage>  list = page.getRecords();
        if(null == list || list.isEmpty()){
            return resDtoList;
        }
        resDtoList = IterableConverter.convertList(list,ImChatMessageResponseDto.class);
        return resDtoList;

    }

    /**
     * 通过分页获取ImChatMessage信息实现查找缓存和数据库的方法
     * @param basePage BasePage
     * @return
     * @author suven
     * @date 2021-05-12 17:26:13
     */
    public List<ImChatMessageResponseDto> getImChatMessageByPage(
            ResponseResultList<ImChatMessageResponseDto> responseResultList, BasePage basePage){


        List<ImChatMessageResponseDto> resDtoList = new ArrayList<>();
        if(basePage == null){
            return resDtoList;
        }
        //分页对象        PageHelper
        IPage<ImChatMessage> iPage = new Page<>(basePage.getPageNo(), basePage.getRealPageSize());
        QueryWrapper<ImChatMessage> queryWrapper = checkQueryCondition(basePage);

        IPage<ImChatMessage> page = this.page(iPage, queryWrapper);
        if(null == page){
            return resDtoList;
        }

        List<ImChatMessage>  list = page.getRecords();
        if(null == list || list.isEmpty()){
            return resDtoList;
        }
        list.forEach(imChatMessage -> {
            ImChatMessageResponseDto imChatMessageResponseDto = ImChatMessageResponseDto.build().clone(imChatMessage);

            resDtoList.add(imChatMessageResponseDto);
        });
        responseResultList.setTotal((int) page.getTotal());
        return resDtoList;


    }
    /**
     * 查询条件
     */
    public QueryWrapper<ImChatMessage> checkQueryCondition(BasePage basePage) {
        QueryWrapper<ImChatMessage> queryWrapper = new QueryWrapper<>();
        ImChatMessageRequestDto imChatMessageRequestDto = (ImChatMessageRequestDto) basePage.getParamObject();

        return queryWrapper;
    }
}